/**
  * Scrolling plane with perspective projection
  *
  */

#include "game.h"

// for readability, the screen and texture sizes are hard-coded
const float SCREEN_WIDTH=640.0f;
const float SCREEN_HEIGHT=360.0f;

const float TEXTURE_WIDTH=128.0f;
const float TEXTURE_HEIGHT=128.0f;

const float SCROLL_SPEED=500.0f;

const VGfloat backgroundColor[4] = { 0.0f, 0.2f, 0.3f, 1.0f };

const float z = 0.25;

// projection matrix, which is dynamically updated for scrolling
VGfloat matrix[3*3]={
    SCREEN_WIDTH/TEXTURE_WIDTH*z, 0, 0,
    SCREEN_WIDTH/TEXTURE_WIDTH/4.0, SCREEN_HEIGHT/TEXTURE_HEIGHT/4.0, 0.5/(TEXTURE_WIDTH),
    0, 3, z,
};


VGImage image;

/** walltime */
float t;

void game_init()
{
}

void game_destroy()
{
}

void game_prepare()
{
    vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_FASTER);
    vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_NONANTIALIASED);

    image = loadVGImage("images/test.png");
}

void game_release()
{
    vgDestroyImage(image);
}

void game_update(float walltime)
{
    t=walltime;
}

void game_render()
{
    // clear background
    vgSetfv(VG_CLEAR_COLOR, 4, backgroundColor);
    vgClear(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
    for(float x=-SCREEN_WIDTH-fmod(t*SCROLL_SPEED, SCREEN_WIDTH);x<=2*SCREEN_WIDTH;x+=SCREEN_WIDTH)
    {
        matrix[6]=x*z;  // X scrolling
        vgLoadMatrix(matrix);
        vgDrawImage(image);
    }
}

void game_event(int type, float x, float y, float z)
{
}

